﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Client_Server_Interface;
using BTM_Client_Application.SupportClasses;

namespace BTM_Client_Application
{
    /// <summary>
    /// Логика взаимодействия для MyTreeViewItem.xaml
    /// </summary>
    public partial class MyTreeViewItemHeader : UserControl
    {

        private ErrorsState _state = ErrorsState.NoErrors;

        private MyTreeViewItemHeader()
        {
            InitializeComponent();
        }

        public MyTreeViewItemHeader(DeviceTreeItem.ItemType itemType, string header) : this()
        {
            TitleBox.Text = header;
            switch (itemType)
            {
                case DeviceTreeItem.ItemType.Region:
                    TitleIcon.Source = GetImageFromResources("direction_16");
                    break;
                case DeviceTreeItem.ItemType.Station:
                    TitleIcon.Source = GetImageFromResources("antenna_16");
                    break;
                case DeviceTreeItem.ItemType.Device:
                    TitleIcon.Source = GetImageFromResources("device_16");
                    break;
            }
        }

        public void SetStateImage(ErrorsState state)
        {
            if (_state == state)
                return;
            switch (state)
            {
                case ErrorsState.HaveSeen:
                    StateIconGrid.Children.Clear();
                    StateIconGrid.Children.Add(new Image { Source = GetImageFromResources("Map-Marker-Ball-Azure_16") });
                    break;
                case ErrorsState.HaveUnseen:
                    StateIconGrid.Children.Clear();
                    StateIconGrid.Children.Add(new Image { Source = GetImageFromResources("Map-Marker-Ball-Pink_16") });
                    break;
                case ErrorsState.NoErrors:
                    StateIconGrid.Children.Clear();
                    break;
            }
        }

        public void OnDeviceErrorsListChanged(object sender, EventArgs e)
        {
            var dev = sender as IDeviceInterface;

            Dispatcher.Invoke(new Action(() =>
            {
                if (dev.UnreadCount > 0)
                {
                    //Background = Res.ErrorBrush;
                    SetStateImage(ErrorsState.HaveUnseen);
                    //_tb.Foreground = new SolidColorBrush(Colors.White);
                }
                else
                {
                    if (dev.ErrorsList.Count > 0)
                    {
                        //Background = Res.HaveSeenErrorsBrush;
                        SetStateImage(ErrorsState.HaveSeen);
                        TitleBox.Foreground = Foreground;
                    }
                    else
                    {
                        //Background = Res.OkBrush;
                        SetStateImage(ErrorsState.NoErrors);
                        TitleBox.Foreground = Foreground;
                    }
                }
            }));
        }

        public override string ToString()
        {
            return TitleBox.Text;
        }

        private static BitmapImage GetImageFromResources(string resourceName)
        {
            var im = new BitmapImage();
            im.BeginInit();
            im.UriSource = new Uri("pack://application:,,,/BTM_Client_Application;component/Images/General/" + resourceName + ".png");
            im.EndInit();
            return im;
        }

        public new Brush Foreground
        {
            get { return TitleBox.Foreground; }
            set { TitleBox.Foreground = value; }
        }

        /*
        public Brush Background
        {
            get { return ContentPanel.Background; }
            set { ContentPanel.Background = value; }
        }*/

    }
}
